<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--学生历年成绩分析
class ExamChartStudent extends Base {
	
	public function index() {
		$fields = "姓名,学号,考试名称,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,总分级排名";
		$list = explode(",", $fields);
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function rankChart() {
		$studentId = input("xsid");
		$result = Db::table("学生信息")->field("ID,姓名,年级号,学号")->where("ID=".$studentId)->find();
		$result = findResult($result);
		$result["学号"] = intval($result["学号"]) > 10 ? $result["学号"] : "0".$result["学号"];
		$this->assign("student", $result);
		return $this->fetch();
	} 

	public function scoreChart() {
		$studentId = input("xsid");
		$result = Db::table("学生信息")->field("ID,姓名,年级号,学号")->where("ID=".$studentId)->find();
		$result = findResult($result);
		$result["学号"] = intval($result["学号"]) > 10 ? $result["学号"] : "0".$result["学号"];
		$this->assign("student", $result);
		return $this->fetch();
	} 

	public function pageQuery() {
		$studentNames = input("studentNames");
		$studentId = input("studentId");
		if(!$studentId && !$studentNames) return [];
		$grade = input("grade");
		$page = input("page");
		$rows = input("rows");
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$where["总分"] = array("gt", 0);
		if($studentNames) {
			$where["姓名"] = array("in", explode(",", $studentNames));
		}else {
			$where["学生ID"] = array("eq", $studentId);
		}
		$result = Db::table("成绩".$grade)->alias("a")
				  ->join("考试名称 b","a.考试ID=b.ID","INNER")
				  ->join("学期名称 c","b.学期ID=c.ID","INNER")
				  ->join("年级名称 d","b.年级ID=d.ID","INNER")
				  ->field("a.考试ID,姓名,学号,年级名称+考试名称 as 考试名称,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,级排 as 总分级排名")
				  ->where($where)
				  ->order("考试ID")
				  ->page($page, $rows)
				  ->select();
		$count = Db::table("成绩".$grade)->alias("a")
				  ->join("考试名称 b","a.考试ID=b.ID","INNER")
				  ->join("学期名称 c","b.学期ID=c.ID","INNER")
				  ->join("年级名称 d","b.年级ID=d.ID","INNER")
				  ->where($where)
				  ->count();
		$grid["rows"] = selectResult($result);
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$studentNames = input("studentNames");
		$studentId = input("studentId");
		if(!$studentId && !$studentNames) return [];
		$grade = input("grade");
		$page = input("page");
		$rows = input("rows");
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$where["总分"] = array("gt", 0);
		if($studentNames) {
			$where["姓名"] = array("in", explode(",", $studentNames));
		}else {
			$where["学生ID"] = array("eq", $studentId);
		}
		$result = Db::table("成绩".$grade)->alias("a")
				  ->join("考试名称 b","a.考试ID=b.ID","INNER")
				  ->join("学期名称 c","b.学期ID=c.ID","INNER")
				  ->join("年级名称 d","b.年级ID=d.ID","INNER")
				  ->field("a.考试ID,姓名,学号,年级名称+考试名称 as 考试名称,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,级排 as 总分级排名")
				  ->where($where)
				  ->order("考试ID")
				  ->select();
		$result=selectResult($result);
		//excel表头内容
		$fields="姓名,学号,考试名称,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,总分级排名";
		$list = explode(",", $fields);
		$header = [];
		foreach ($list as $key => $value){
			$header[$value] = $value;
		}
		Excel::toExcel($result,'学生历年成绩',$header);
		exit;
	}
}
